package com.joyque.androidbug.report.service.impl;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.joyque.androidbug.report.dao.BugRepository;
import com.joyque.androidbug.report.entity.Application;
import com.joyque.androidbug.report.entity.Bug;
import com.joyque.androidbug.report.service.BugService;

@Service
@Transactional
public class BugServiceImpl implements BugService {

	@Inject
	private BugRepository bugRepository;

	@Override
	public Bug saveBug(String content, String device_id, String system_version, Application app, String code_version, String pId) {
		Bug bug = bugRepository.findByPId(pId);
		if(bug ==null){
			bug =parseBug(content, device_id, system_version, app, code_version, pId);
			bugRepository.save(bug);
		}
		return bug;
	}

	private Bug parseBug(String content, String device_id, String system_version, Application app, String code_version, String pId) {
		Bug bug = new Bug();
		bug.setApplication(app);
		bug.setContent(content);
		bug.setDeviceId(device_id);
		bug.setSystemVersion(system_version);
		bug.setCodeVersion(code_version);
		bug.setCreatedAt(new Date());
		bug.setpId(pId);
		return bug;
	}

}
